﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAL.Entities;
using System.Data.SqlClient;

namespace DAL.EntitiesDAL
{
    public class CosuphamDAL : BaseDAL, UTL.IBaseDAL
    {
        #region Implement

        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            return ExecuteQuery("sp_SelectAll_Cosupham");
        }

        public DataTable Select_Supham()
        {
            return ExecuteQuery("sp_SelectAll_Supham");
        }

        public DataTable Select_Chungchisupham()
        {
            return ExecuteQuery("sp_SelectAll_Chungchisupham");
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            var sql = "sp_Delete_Cosupham '{0}'";
            sql = string.Format(sql, id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Delete_Supham(string id, string id1)
        {
            var sql = "sp_Delete_Cosupham '{0}', '{1}'";
            sql = string.Format(sql, id, id1);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (Cosupham)obj;
            var sql = "set dateformat dmy; execute sp_Insert_Cosupham '{0}', '{1}', N'{2}', '{3}', N'{4}' ";
            sql = string.Format(sql, o.MA_TDSP, o.MANHANVIEN, o.CHUNGCHI_SP, o.NGAYCAP_CCSP.ToString("dd/MM/yyyy"), o.NOICAP_CCSP);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (Cosupham)obj;
            var sql = "set dateformat dmy; execute sp_Update_Cosupham '{0}', '{1}', N'{2}', '{3}', N'{4}' ";
            sql = string.Format(sql, o.MA_TDSP, o.MANHANVIEN, o.CHUNGCHI_SP, o.NGAYCAP_CCSP.ToString("dd/MM/yyyy"), o.NOICAP_CCSP);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            var sql = @"Select * from tb_Cosupham";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }

        public DataTable Search(DAL.Entities.Cosupham o)
        {
            string sql;
            if (o.MA_TDSP != "")
                sql = @"Select * from tb_Cosupham Where MA_TDSP like '%" + o.MA_TDSP + "%'  ";
            else if (o.MANHANVIEN != "")
                sql = @"Select * from tb_Cosupham Where MANHANVIEN like '%" + o.MANHANVIEN + "%'";
            else if (o.CHUNGCHI_SP != "")
                sql = @"select * from tb_Cosupham where CHUNGCHI_SP like N'%" + o.CHUNGCHI_SP + "%' ";
            else
                sql = "@select * from tb_Cosupham where NOICAP_CCSP like '%" + o.NOICAP_CCSP + "%' ";

            return ExecuteQuery(sql);
        }


        #endregion
    }
}
